package com.steven.team.controller;

import com.alibaba.fastjson.JSONObject;
import com.steven.team.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class FriendController {

    @Autowired
    FriendService friendService;

    @PostMapping("/friend/query")
    public JSONObject queryFriend(@RequestHeader Long uid) {
        return friendService.queryFriends(uid);
    }

    @PostMapping("/friend/apply")
    public JSONObject applyFriend(@RequestHeader Long uid, Long toUser) {
        return friendService.applyFriend(uid, toUser);
    }

    @PostMapping("/friend/updateApplication")
    public JSONObject updateApplication(@RequestHeader Long uid, Long applicantUser, boolean agree) {
        updateToUserApplicantReadStatus(uid);
        if (agree) {
            return friendService.agreeFriend(uid, applicantUser);
        } else {
            return friendService.refuseFriend(uid, applicantUser);
        }
    }

    @PostMapping("/friend/queryApplicationToSelf")
    public JSONObject queryApplicationToSelf(@RequestHeader Long uid) {
        updateToUserApplicantReadStatus(uid);
        return friendService.queryApplicationToSelf(uid);
    }

    @PostMapping("/friend/querySelfApplication")
    public JSONObject querySelfApplication(@RequestHeader Long uid) {
        return friendService.querySelfApplication(uid);
    }

    @PostMapping("/friend/ApplicantReadStatus/updateToUser")
    public JSONObject updateToUserApplicantReadStatus(@RequestHeader Long uid) {
        return friendService.updateToUserApplicantReadStatus(uid);
    }

    @PostMapping("/friend/delete")
    public JSONObject deleteFriend(@RequestHeader Long uid, Long friendId) {
        return friendService.deleteFriend(uid, friendId);
    }

    @PostMapping("/user/detail")
    public JSONObject queryUserDetail(@RequestHeader Long uid, Long friendId, HttpServletResponse response) throws IOException {
        if (uid == null) {
            response.sendError(500, "传参错误");
            return null;
        }
        return friendService.queryUserDetail(friendId, uid);
    }

    @PostMapping("/friend/chatHistory")
    public JSONObject queryChatFriends(@RequestHeader Long uid,HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        return friendService.queryChatFriends(uid);
    }
}
